package com.ch999.detect.View.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ch999.detect.R;
import com.ch999.detect.View.NewDetectView;
import com.ch999.detect.View.SelectStartDialog;
import com.ch999.detect.View.activity.NewDetectActivity;
import com.ch999.detect.mode.bean.DialogCustomMode;
import com.ch999.detect.presenter.NewDetectPresenter;


/**
 * 自定义通用dialog
 * Created by Administrator on 2016/12/13 0013.
 */

public class MyCustomDialog extends SelectStartDialog {

    private TextView tvTitle;
    private TextView tvOk;
    private TextView tvNo;
    private TextView tvCountDown;//倒计时

    private LinearLayout llCustomView;//自定义view
    private View.OnClickListener listener;
    private View buttom;

    NewDetectView mView;
    NewDetectPresenter mPresenter;

    public MyCustomDialog(Context context, View.OnClickListener listener, NewDetectActivity mView, NewDetectPresenter mPresenter) {
        super(context);
        this.listener = listener;
        this.mView = mView;
        this.mPresenter = mPresenter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom);

        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvOk = (TextView) findViewById(R.id.tvOk);
        tvNo = (TextView) findViewById(R.id.tvNo);
        tvCountDown = (TextView) findViewById(R.id.tvCountDown);
        llCustomView = (LinearLayout) findViewById(R.id.llCustomView);
        buttom = findViewById(R.id.ll_bottom);

        findViewById(R.id.tvOk).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onClick(view);
                }
            }
        });
        findViewById(R.id.llCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    if (mPresenter.isVolumekeyUP()) {
                        mPresenter.setVolumekeyUP(false);
                        mPresenter.setVolumekeyDOWN(true);
                        mView.closeDialog(false);
                        NewDetectPresenter.Fucation inflate = mPresenter.getInflate();
                        inflate.tvFucation.setText("请按音量“-”键");
                        inflate.imgIcon.setImageResource(R.mipmap.icon_audio_dric);
                        View view1 = inflate.rootView;
                        mView.showDialog(new DialogCustomMode("按键功能", view1), true);
                    } else {
                        listener.onClick(view);
                    }
                }
            }
        });

    }

    /**
     * 倒计时
     *
     * @param count
     */
    public void setCountDown(long count) {
        if (tvCountDown != null) {
            tvCountDown.setText("（" + count + "秒）");
        }
    }

    public void setTvTitle(String title) {
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    public void setTvOkText(String text) {
        if (tvOk != null) {
            tvOk.setText(text);
        }
    }

    public void setTvNoText(String text) {
        if (tvNo != null) {
            tvNo.setText(text);
        }
    }

    public void setNeedCountDown(boolean needCountDown) {
        if (!needCountDown) {
            tvCountDown.setText("");
        }
    }

    public void setIsShowButtom(boolean isShowButtom) {
        buttom.setVisibility(isShowButtom ? View.VISIBLE : View.GONE);
    }

    public void setShowBtnOk(boolean isShowBtnOk) {
        tvOk.setVisibility(isShowBtnOk ? View.VISIBLE : View.GONE);
    }

    /**
     * 添加自定义view
     *
     * @param view
     */
    public void setCustomView(View view) {
        if (llCustomView != null) {
            llCustomView.removeAllViews();
            llCustomView.addView(view);
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {


            case KeyEvent.KEYCODE_VOLUME_UP:
                //音量键“+”
                if (mPresenter.isVolumekeyUP()) {
                    mPresenter.setVolumekeyUP(false);
                    mPresenter.setVolumekeyDOWN(true);
                    mView.closeDialog(false);
                    NewDetectPresenter.Fucation inflate = mPresenter.getInflate();
                    inflate.tvFucation.setText("请按音量“-”键");
                    inflate.imgIcon.setImageResource(R.mipmap.icon_audio_dric);
                    View view = inflate.rootView;
                    mView.showDialog(new DialogCustomMode("按键功能", view), true);
                }


                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                //音量键“-”
                if (mPresenter.isVolumekeyDOWN()) {
                    mView.detectSuccess(0);
                    mView.closeDialog(false);
                    mPresenter.setVolumekeyDOWN(false);
                }


                return true;

        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void show() {
        super.show();
    }
}
